/**
 * @author ahammer
 * @date   2009
 * @update 2010 05 added gif format support
 * @update 2010 06 added fits format support
 * @update 2010 08 added dcm format support
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package main;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import tools.ImagePreviewJPanel;

public class Open {
	
	private static File currImgDir;
	
	public static void run() {
		JFileChooser fc = null;
		//JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));// im eclipse system

		try {fc = new JFileChooser();
		}
		catch (Throwable e) {
			System.out.println("Open: Problem with JfileChooser");
			return;
		}
	
		fc.setMultiSelectionEnabled(true);
		fc.setDialogTitle("Open image(s)");
		FileNameExtensionFilter filt     = new FileNameExtensionFilter("Image files", "tif", "tiff", "jpg", "jpeg", "png", "bmp", "gif", "dcm", "fits");
		FileNameExtensionFilter filtTIFF = new FileNameExtensionFilter("tif", "tif");		 		
		fc.addChoosableFileFilter(filt);
	    fc.addChoosableFileFilter(filtTIFF);
	    fc.addChoosableFileFilter(new FileNameExtensionFilter("jpg", "jpg"));
	    fc.addChoosableFileFilter(new FileNameExtensionFilter("png", "png"));
	    fc.addChoosableFileFilter(new FileNameExtensionFilter("bmp", "bmp"));
	    fc.addChoosableFileFilter(new FileNameExtensionFilter("gif", "gif"));
	    fc.addChoosableFileFilter(new FileNameExtensionFilter("dcm", "dcm"));
	    fc.addChoosableFileFilter(new FileNameExtensionFilter("fits", "fits"));
	    
	    fc.setFileFilter(filt);        //default setting
	    //fc.setFileFilter(filtTIFF);  //default setting
		
        currImgDir  = IqmTools.getCurrImgPath();
        //System.out.println("Open: " + defaultDir.toString());
		fc.setCurrentDirectory(currImgDir);
	    
		//Add Thumbnail Preview
		//Thanks to Jakob Hatzl FH Joanneum Graz for this link:
		//http://www.javalobby.org/java/forums/t49462.html
		ImagePreviewJPanel preview = new ImagePreviewJPanel();
		fc.setAccessory(preview);
		fc.addPropertyChangeListener(preview);

		
		int returnVal = fc.showOpenDialog(fc);
		if (returnVal!=JFileChooser.APPROVE_OPTION) {
			System.out.println("Open: Error: No image(s) opened");
			return;
		}
		File[] files = fc.getSelectedFiles();	
		if (files.length == 0) { // 
			files = new File[1];
			files[0] = fc.getSelectedFile();
		}
		//set default directory
		currImgDir = fc.getCurrentDirectory();
		IqmTools.setCurrImgPath(currImgDir);
		Open.open(files);
	}	
		
	public static void open (File[] files) {
		//System.out.println("Open: image(s): ");			
		Board.appendTexln("Open: image(s): ");			
		for (int i = 0; i < files.length; i++){
			Board.appendTex(files[i].getName().toString()+", ");
		}	
		Board.appendTexln(" ");	
		//Arrays.sort(files);
		// Sort files by name.
//		Arrays.sort(files, new Comparator()
//		{
//		    public int compare(Object o1, Object o2)
//		    {
//			return ((File) o1).getName().compareTo(((File) o2).getName());
//		    }
//		});
		//String path = fc.getCurrentDirectory().getPath()+Prefs.getFileSeparator();
		//String path = fc.getCurrentDirectory().getPath() +System.getProperty("file.separator");
	    
	
		currImgDir = IqmTools.getCurrImgPath();
		
		//Save property to iqm.ini file
		IqmTools.setIqmIniFile("CurrImgPath", currImgDir.toString());
	
	    Tank.addNewItem(files);
		
	}

}
